package com.alibaba.otter.manager.web.codegen;

import java.sql.*;
import java.time.Instant;

public class BatchInserts {

    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/minipro?useSSL=false&serverTimezone=UTC";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        // SQL语句（使用预编译语句防止SQL注入）
        String sql = "INSERT INTO `jhi_user_copy1` "
                + "( `login`, `password_hash`, `first_name`, `last_name`, "
                + "`email`, `image_url`, `activated`, `lang_key`, `activation_key`, "
                + "`reset_key`, `created_by`, `created_date`, `reset_date`, "
                + "`last_modified_by`, `last_modified_date`) "
                + "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(JDBC_URL,  USERNAME, PASSWORD))  {

            for (int i = 0; i < 30000; i++) {
                PreparedStatement pstmt = conn.prepareStatement(sql);
                // 设置参数
                pstmt.setString(1,  "user" + "_" + i + "_" + i);  // login
                pstmt.setString(2,  "$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K");  // password_hash
                pstmt.setString(3,  "User" + "_" + i+ "_" + i);  // first_name
                pstmt.setString(4,  "User" + "_" + i+ "_" + i);  // last_name
                pstmt.setString(5,  "user@localhost" + "_" + i + "_" + i);  // email
                pstmt.setString(6,  "");  // image_url
                pstmt.setBoolean(7,  true);  // activated (b'1'对应true)
                pstmt.setString(8,  "zh-cn");  // lang_key
                pstmt.setNull(9,  java.sql.Types.VARCHAR);   // activation_key (NULL)
                pstmt.setNull(10,  java.sql.Types.VARCHAR);   // reset_key (NULL)
                pstmt.setString(11,  "system");  // created_by
                pstmt.setTimestamp(12, Timestamp.from(Instant.now()));   // created_date (NULL)
                pstmt.setTimestamp(13,  Timestamp.from(Instant.now()));   // reset_date (NULL)
                pstmt.setString(14,  "system");  // last_modified_by
                pstmt.setTimestamp(15,  Timestamp.from(Instant.now()));   // last_modified_date (NULL)

                // 执行插入
                int affectedRows = pstmt.executeUpdate();
                System.out.println(" 插入完成，影响行数: " + affectedRows);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
